package com.dongxing.concyrrency.chapter5;

import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

/**
 * @author XavierDong
 **/
public class ThreadJoin2 {

    public static void main(String[] args) {


        Thread t1 = new Thread(() -> {

            try {
                System.out.println("t1 is running.");
                TimeUnit.SECONDS.sleep(10);
                System.out.println("t1 is done.");
            } catch (InterruptedException e) {
                e.printStackTrace();
                Thread.currentThread().interrupt();
            }
        });
        t1.start();
        try {
            // 等待多长时间，默认的是0，等待t1完全结束，才运行main线程
            t1.join(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Optional.of("All of tasks finish done.").ifPresent(System.out::println);
        IntStream.range(1, 1000).forEach(i -> System.out.println(Thread.currentThread().getName() + i));

    }
}